package com.haoxin.ai_course.config;
 
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;

import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.paths.RelativePathProvider;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {
 
 
    @Bean
    public Docket defaultApi2(ServletContext servletContext) {
    	  ParameterBuilder tokenPar = new ParameterBuilder();
    	  List<Parameter> pars = new ArrayList<Parameter>();
          tokenPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
          pars.add(tokenPar.build());
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //分组名称
                .groupName("1.0版本")
                .select()
                //这里指定Controller扫描包路径(项目路径也行)
                .apis(RequestHandlerSelectors.basePackage("com.haoxin.ai_course.modules.course"))
                .paths(PathSelectors.any())
                .build().globalOperationParameters(pars);
        docket.pathProvider(new RelativePathProvider(servletContext) {
            @Override
            public String getApplicationBasePath() {
//            	String path = "/ai_course" + super.getApplicationBasePath()+"apidoc/";
            	String path = "/ai_course" + super.getApplicationBasePath();
                return path;
            }
        });
        return docket;
        
        
        
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口说明")
                .description("DEMO服务接口说明")
                .termsOfServiceUrl("https://www.hryrxr.cn/ai_course/apidoc/doc.html")
                .version("1.0").extensions(null)
                .build();
    }
}